PAT甲級 1029median 參考柳神解題思路

先扔完整代碼,可通過在線測試,具體遲點再寫

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int m, n;
	cin >> m;
	vector<int> arr1(m);
	for (int i = 0; i < m; i++)
	{
		cin >> arr1[i];
	}
	cin >> n;
	int total = (m + n + 1) / 2;
	int count = 0;
	int i = 0;
	for (int j = 0; j < n; j++)
	{
		int temp = 0;
		cin >> temp;
		while (i < m&&arr1[i] < temp)
		{
			count++;
			if (count == total)
			{
				cout << arr1[i];
			}
			i++;
		}
		count++;
		if (count == total)
		{
			cout << temp;
		}
	}
	while (i < m&&count<total)
	{
		count++;
		if (count == total)
		{
			cout << arr1[i];
			return 0;
		}
		i++;
	}
}

想研究的可以先去看柳神的解答

柳神網站:https://www.liuchuo.net/archives/2248

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章